package com.jackrain.nea.web.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.schema.Table;
import com.jackrain.nea.core.schema.TableManager;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.service.AjaxUtils;
import com.jackrain.nea.util.Tools;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.PortletConfig.ObjectUIConfig;
import com.jackrain.nea.web.PortletConfig.PortletConfig;
import com.jackrain.nea.web.PortletConfig.PortletConfigManager;
import com.jackrain.nea.web.face.impl.UserImpl;
import com.jackrain.nea.web.query.*;
import com.jackrain.nea.web.security.Directory;
import com.jackrain.nea.web.security.SecurityWebUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(value = "TableQueryController", description = "列表查询数据rest")
public class QueryListController {

    
    @Autowired
    private PropertiesConf pconf;


//    @Autowired
//    private AjaxUtils ajaxUtils;


    @RequestMapping(path = "/p/cs/QueryList", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject getTableQuery(HttpServletRequest request,
                                    @RequestParam(value = "searchdata", required = true) String obj) throws Exception {
        log.debug("query data ->("+ obj+")");
        ValueHolder valueHolder = new ValueHolder();
        
        try {
            JSONObject jo=JSON.parseObject(obj);
            UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");
            QuerySession qsession = QueryUtils.createQuerySession(userWeb);
            TableManager tm = qsession.getTableManager();
    
            Table table=null;
            
            int tableid=jo.getIntValue("tableid") ;
            int refcolid=jo.getIntValue("refcolid") ;
    
            if(refcolid>0) {
                table = tm.getColumn(refcolid).getReferenceTable();
            }
            
            if(tableid>0&&table==null) {
                table = tm.getTable(tableid);
            }
            
            if(jo.getString("table")!=null&&table==null) {
                table = tm.getTable(jo.getString("table"));
            }
            
            
            if(table==null) {
                throw  new NDSException("tablenull");
            }

            if(!userWeb.isAdmin()) {
                SecurityWebUtils.checkPermission(table.getSecurityDirectory(tm), userWeb.getId(), Directory.READ);
            }
            
            QueryRequest query=AjaxUtils.parseQuery(jo, qsession, userWeb.getId(), userWeb.getLocale(),table);
            log.debug("query sql->"+query.toSQL());

            log.debug("query FullRange sql->"+query.toFullRangeSubTotalSQL());
            log.debug("query Range sql->"+query.toSQLWithRange());

            QueryResult qr= QueryEngine.getInstance().doQuery( query);

            JSONObject result=qr.getColumnDataObj(true);
            result.put("tabth",query.SelectbyDisObj(true,userWeb.getLocale()));
            result.put("tabname",table.getName());

            String rangeconf=pconf.getProperty("query.select.range","10,20,50,100");

            JSONArray selectrange= (JSONArray) JSON.toJSON(Tools.parseIntArray(rangeconf,","));
            result.put("selectrange",selectrange);
            result.put("defaultrange",query.getRange());



            ObjectUIConfig objConfig=(ObjectUIConfig) PortletConfigManager.getInstance().getPortletConfig(table.getUIConfigId(), PortletConfig.TYPE_OBJECT_UI);
            
                    
            //明细打开单对象样式
            result.put("objdistype",objConfig.getObjdisplay());
            
            valueHolder.put("code", 0);
            valueHolder.put("datas", result);
            

        }catch (Exception e) {
            e.printStackTrace();
            valueHolder.put("code", -1);
            valueHolder.put("msg", e.getMessage());
            return valueHolder.toJSONObject();
        }




        return valueHolder.toJSONObject();
    }



}
